package fr.umlv.jee.jawp.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.jee.jawp.persistence.dao.ICategoryDAO;
import fr.umlv.jee.jawp.persistence.object.Category;

@Service("categoryService")
public class CategoryService {

	@Resource(name = "categoryDAO")
	private ICategoryDAO dao;

	/*************************************
	 * Creation
	 *************************************/

	@Transactional
	public Category createNewCategory(String name, int motherCategoryId) {
		return dao.createNewCategory(name, motherCategoryId);
	}

	@Transactional
	public Category createNewCategory(String name) {
		return dao.createNewCategory(name);
	}

	/*************************************
	 * Getter
	 *************************************/

	@Transactional(readOnly = true)
	public List<Category> getCategories() {
		return dao.getCategories();
	}

	@Transactional(readOnly = true)
	public int getNbCategories() {
		return dao.getCategories().size();
	}

	@Transactional(readOnly = true)
	public List<Category> getCategoriesByMotherId(int motherCategoryId) {
		return dao.getCategoriesByMotherId(motherCategoryId);
	}

	@Transactional(readOnly = true)
	public List<Category> getCategoriesByMotherId() {
		return dao.getCategoriesByMotherId();
	}

	@Transactional(readOnly = true)
	public Category getCategoriesById(int id) {
		return dao.getCategoriesById(id);
	}

	@Transactional(readOnly = true)
	public Category getCategoryByNameAndMotherCategory(String name, int categoryId) {
		return dao.getCategoryByNameAndMotherCategory(name, categoryId);
	}

	@Transactional(readOnly = true)
	public Category getCategoryByNameAndMotherCategory(String name) {
		return dao.getCategoryByNameAndMotherCategory(name);
	}

	@Transactional(readOnly = true)
	public String getCategoryTreeStructure(Category category) {
		return dao.getCategoryTreeStructure(category);
	}

	/*************************************
	 * Setter
	 *************************************/

	@Transactional
	public void saveCategory(Category category) {
		dao.saveCategory(category);
	}

	@Transactional
	public void saveCategories(List<Category> categories) {
		dao.saveCategories(categories);
	}

	/*************************************
	 * Deletion
	 *************************************/

	@Transactional
	public void deleteCategory(int id) {
		dao.deleteCategory(id);
	}

	@Transactional
	public void clearDB() {
		dao.clearDB();
	}
}
